/*
 * Copyright (C) 2021 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.view.ViewCompat;

import com.xuexiang.xui.utils.DensityUtils;

public class Microphone extends View {

    public Microphone(Context context) {
        super(context);
    }

    public Microphone(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Microphone(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    RectF mBounds = new RectF();
    Paint mPaint = null;
    float RADIUS = DensityUtils.dp2px(getContext(), 150);
    float radius[] = {0f,0f,0f,0f};

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mBounds.set(w / 2 - RADIUS, h / 2 - RADIUS, w / 2 + RADIUS, h / 2 + RADIUS);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAlpha(255);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.parseColor("#72777b"));
        mPaint.setStrokeWidth(30);

        for(int i=0;i<4;i++) {
            if(radius[i] != 0f && radius[i] < 1f) {
                canvas.drawArc(boundsByRadius(radius[i] * RADIUS), 225, 90, false, mPaint);
            }
        }
    }

    public RectF boundsByRadius(float radius) {
        int h = getHeight();
        int w = getWidth();
        mBounds.set(w / 2 - radius, h / 2 - radius, w / 2 + radius, h / 2 + radius);
        return mBounds;
    }
    ValueAnimator animtor = ValueAnimator.ofFloat(0f,1f);
    public void start() {

        long time = 1500;

        animtor.setInterpolator(new AccelerateDecelerateInterpolator());
        animtor.setDuration(time);
        animtor.setRepeatCount(-1);
        animtor.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float updateValue = (float) animation.getAnimatedValue();
                for(int i=0;i<4;i++) {
                    radius[i] = updateValue + (0.25f * i);
                }
                ViewCompat.postInvalidateOnAnimation(Microphone.this);
            }

        });
        animtor.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });
        animtor.start();
    }

    public void stop() {
        animtor.setRepeatCount(0);
        /*
        for(int i=0;i<4;i++)
            radius[i] = 0f;
        */
    }
}
